package dev.ece.suit.hdr;

import dev.ece.core.cells.model.base.FboUI;
import dev.ece.core.cells.model.base.SquareUI;
import dev.ece.core.sprite.Component;
import dev.ece.core.texture.FBO;
import dev.ece.util.tools.Camera;

/**
 * @author 潜行的青衣
 * @see
 */
public class HDRComponent extends Component<Float, SquareUI<Float>> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1682005073923998846L;

	private HDRShader hdrShader;
	
	private FBO hdr;
	
	private float gamma = 1.0f;
	
	private float exposure = 1.0f;
	
	private boolean enable;
	
	public HDRComponent(HDRShader hdrShader, int width, int height) throws Exception {
		this(new FboUI(width, height), hdrShader, FBO.newHDRInstance(width, height));
	}
	
	/**
	 * @param model
	 */
	public HDRComponent(SquareUI<Float> model, HDRShader hdrShader, FBO hdr) {
		super(model);
		this.hdrShader = hdrShader;
		this.hdr = hdr;
		this.initialize();
	}

	@Override
	public void onRender(Camera camera) {
		this.hdrShader.glUseProgram();
		this.hdrShader.glModelViewProjectMatrix();
		this.hdrShader.glEnable(isEnable());
		this.hdrShader.glTexture(getHdr().getTexture());
		this.hdrShader.glExposure(getExposure());
		this.hdrShader.glGamma(getGamma());
		super.onRender(camera);
	}

	public FBO getHdr() {
		return hdr;
	}

	public float getExposure() {
		return exposure;
	}

	public void setExposure(float exposure) {
		this.exposure = exposure;
	}

	public float getGamma() {
		return gamma;
	}

	public void setGamma(float gamma) {
		this.gamma = gamma;
	}

	public boolean isEnable() {
		return enable;
	}

	public void setEnable(boolean enable) {
		this.enable = enable;
	}

	@Override
	public float getWidth() {
		return this.getUi().getWidth();
	}

	@Override
	public float getHeight() {
		return this.getUi().getHeight();
	}

	public void begin() {
		this.getHdr().begin();
	}
	
	@Override
	public void destroy() {
		super.destroy();
		hdr.destroy();
		hdr = null;
	}

}
